#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

use LWP::Simple;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

my $guid = 'ddb30831-0f43-4e42-8c76-33baf56f1b36';
my $urlPrefix = 'http://questionablecontent.net/view.php?comic=';
my $escapedUrlPrefix = quotemeta($urlPrefix);
my $archiveUrl = 'http://questionablecontent.net/archive.php';
#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid);
my $comicName = $comic->getName();

my $lastStripRow = $comic->getLastStrip();
my $archivePage = '';

my $dateParser = new DateTime::Format::Strptime(
	pattern => '%B %d, %Y',
	locale  => 'en_US'
);

# use current time since no dates anywhere on site
my $strip = time;

# use previously calculated strip values since no dates anywhere on site
#open(STRIPS, "QCstrip.txt") or die "$comicName: $!";
#my @strips;
#push @strips, -1; # filler since there is no 0th strip
#for my $strip (<STRIPS>) {
#	push @strips, int($strip);
#}
#close STRIPS;

# special cases for titles (makes me cry inside)
my %specialTitles = (
	 160 => 'Comic 160: QC Guest Week: Julie Keene', # listed twice (2x)
	 436 => 'Comic 436: I Am Your Father', # 2x
	 503 => 'Comic 503: The Talk, Part 4', # 2x
	 525 => 'Comic 525: Funny Ha Ha', # 2x
	 569 => 'Comic 569: Cocoa Is A Good Peace Offering', # see 570
	 570 => 'Comic 570: She Missed It All', # listed as "Comic 569: ..."
	 846 => 'Comic 846: I Wish I Could Do Humor So Concisely', # 2x
	 870 => 'Comic 870: Semi-Nakers!', # not listed
	 931 => 'Comic 931: This One Goes Out To My Homies At Harmonix', # 2x
	 995 => 'Comic 995: Jeph-Is-Sick-Guest-Comic-Action: Pop-Culture Boners', # 2x
	1152 => 'Comic 1152: Orders From On High', # 2x
	1226 => 'Comic 1226: Lethal Combination', # 2x
	1285 => 'Comic 1285: An Impolite Interruption', # 3x
	1296 => 'Comic 1296: Board Certified And Everything', # 2x
	1305 => 'Comic 1305: All In The Family', # 6x
	1386 => 'Comic 1386: Also Tiger Woods Is Really Good At It', # 2x
	1403 => 'Comic 1403: Punk\'d', # 2x
	1407 => 'Comic 1407: QC Sock Puppet Theatre', # 2x
	1421 => 'Comic 1421: Gullibility Cortex', # 2x
	1448 => 'Comic 1448: It Was Just Too Oedipal', # 2x
	1456 => 'Comic 1456: Yelling Bird Is A Name-Dropping Whore', # 2x
	1495 => 'Comic 1495: Pretty Pretty Pretty Pretty', # 2x
	1496 => 'Comic 1496: Tetsuoooooo! Kanedaaaaaaaaa!', # 2x
	1514 => 'Comic 1514: Wrong Way To Break The Ice', # 2x
	1781 => 'Comic 1781: It Does A Body Good', # 2x
	1804 => 'Comic 1804: OMG Turkeys 2010', # 2x
	1811 => 'Comic 1811: Magic Touch', # 2x
	1835 => 'Comic 1835: The Misfit', # 2x
	1884 => 'Comic 1884: Intruder Alert' #2x
);

sub getId {
	my $url = shift(@_) or die "$comicName: url not passed to getId";
	$url =~ /^$escapedUrlPrefix(\d+)$/ or die "$comicName: malformed url ($url)";
	return int($1);
}

# scrape information from a page
sub scrapePage {
	my $url = shift(@_) or die "$comicName: url not passed to scrapePage";
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	
	my $id = getId($url);
	my $data = {};
	$data->{url} = $url;

	if(defined $specialTitles{$id}) {
		$data->{title} = $specialTitles{$id};
	}
	elsif($archivePage ne '') {
		my $titleRegex = qr/\<a href="view.php\?comic=$id">(.*?)\<\/a\>/;
		my @scraped = $comic->scrapeUnique($archivePage, $titleRegex);
		$data->{title} = $scraped[0];
	}

	# generate 'image' value
	$data->{image} = "http://questionablecontent.net/comics/$id.png";

	return $data;
}


my $stripPage = get($lastStripRow->{url})
                or die "$comicName: last page url not found";
my $lastStripData = scrapePage($lastStripRow->{url}, $stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);


while($stripPage =~ /\<a href="view\.php\?comic=(\d+)"\>Next\<\/a\>/) {
	my $url = $urlPrefix.$1;
	$stripPage = get($url) or die "$comicName: next strip page not found";

	if($archivePage eq '') {
		$archivePage = get($archiveUrl) or die "$comicName: archive page not found";
	}

	#print "$url\n";

	my $data = scrapePage($url, $stripPage);
	
	# if using file for strip values
	#$data->{strip} = $strips[getId($url)];
	
	# if using current time for strip values
	$data->{strip} = $strip;
	$strip += 1000;

	$comic->addStrip($data);
}

$comic->commit();

